<template>
  <div>
    <!-- 账户明细查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @resetChange="resetChange" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTables
        ref="table"
        columnsRef="qjhAccountDetailsQuery"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" :click="exportHandler">导 出</multiuse-button>
        </template>
      </FilterTables>
    </Spin>

    <BaseDialog :visible.sync="isDealer" title="经销商查询" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SearchDealer :rowData="formData" @close="emitCloseHandler1" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, exportExcel, getFiAccountType,exporturl } from '@/api/omFinanceP/MoneyManage/AccountDetailsQuery';
import SearchDealer from '../ownerSeacher';
import { exportExcelFile } from '@/utils/index';
import { getRegion } from '@/api/omFinanceP';

export default {
  name: 'AccountDetailsQuery',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      isDealer: false,
      exportLoading: false,
      deptDictList: []
    };
  },
  components: { SearchDealer },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.accountType();
    this.getDeptData();
  },
  methods: {
    //加载事业部数据
    async getDeptData() {
      const res = await getRegion();
      if (res.resultCode === 200) {
        this.deptDictList = res.data.map(x => ({ codeId: x.orgId, codeCnDesc: x.orgName }));
        res.data.map(x => {
          this.columns.find(item => item.dataIndex == 'orgId').filterItems.push({ text: x.orgName, value: x.orgId });
        });
      }
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    resetChange(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    seacherFun() {
      this.isDealer = true;
    },
    async addHandle(type) {
      if (type == 1) {
        await this.$confirm('是否通过?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {})
          .catch(() => {});
      } else {
        this.isreject = true;
      }
    },

    editHandle(data) {
      this.visible = true;
    },
    emitcloseHandler() {
      this.visible = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    emitCloseHandler1(val, data) {
      this.isDealer = val;
      if (data) {
        let dealerCodes = [];
        let dealerNames = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
          dealerNames.push(x.dealerName);
        });
        this.topFilterList.find(x => x.fieldName == 'ownerCode').initialValue = dealerCodes.toString();
      }
    },
    // 资金类型 getFiAccountType
    async accountType() {
      const res = await getFiAccountType({ ignoreAccountTypes: [60361001, 60361002] });
      if (res.resultCode === 200) {
        res.data.map(x => {
          this.topFilterList.find(item => item.fieldName == 'accountTypeId').itemList.push({ text: x.accountTypeName, value: x.id });
        });
      }
    },
    // 导出
    async exportHandler() {
      //    let json = Object.assign({}, { CODE: 'CODE_0000004' }, this.fetchParams);
      // let res = await exporturl(json);
      // if (res.resultCode == 200) {
      //   this.$router.push('/admin/auth/oem/downloadCenter');
      // }
      await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
        confirmButtonText: '下载',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.exportLoading = true;

          exportExcel(this.fetchParams)
            .then(response => {
              exportExcelFile(response.data, '账户明细查询');
              this.exportLoading = false;
            })
            .catch(err => {

            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '业务类型',
          fieldName: 'businessType',
          placeholder: '请选择',
          itemList: this.createDictList('2112')
        },
        {
          type: 'INPUT',
          label: '业务单据号',
          fieldName: 'billNo',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountTypeId',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '记账时间范围',
          fieldName: 'beginAccountDate|endAccountDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
 {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'ownerName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '流水方向',
          fieldName: 'flowDirectionStr',
          placeholder: '请选择',
          itemList: [
            { text: '增', value: '增' },
            { text: '减', value: '减' }
          ]
        }

      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '流水方向',
          dataIndex: 'flowDirection',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        // {
        //   title: '事业部',
        //   dataIndex: 'orgId',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: [],
        //   render: props => {
        //     return <span>{this.transDictText(props.row.orgId, this.deptDictList)}</span>;
        //   }
        // },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'ownerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '记账时间',
          dataIndex: 'accountDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '业务单据号',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '承兑汇票号',
          dataIndex: 'acceptanceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '业务类型',
          dataIndex: 'businessType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('2112'),
          render: props => {
            return <span>{this.createDictText(props.row.businessType, '2112')}</span>;
          }
        },
        {
          title: '原金额',
          dataIndex: 'originalAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'right',
          precision: 2,
          numberFormat: true
        },

        {
          title: '发生金额',
          dataIndex: 'chngAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'right',
          precision: 2,
          numberFormat: true
        },
        {
          title: '账户余额',
          dataIndex: 'balanceAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'right',
          precision: 2,
          numberFormat: true
        },
        // {
        //   title: '可用金额',
        //   dataIndex: 'availableAmount',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'input',
        //   precision: 2,
        //   numberFormat: true
        // },
        {
          title: '操作人',
          dataIndex: 'updatedByName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '备注（SAP摘要信息）',
          dataIndex: 'remark',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
